package cn.bugDance.infrastructure.gateway.adapter;


import cn.bugDance.domain.security.servive.adapter.IWeiXinLoginAdapter;
import cn.bugDance.infrastructure.gateway.api.IWeiXinApiService;
import cn.bugDance.infrastructure.gateway.dto.WeiXinAccessTokenResponseDTO;
import cn.bugDance.infrastructure.gateway.dto.WeiXinTicketRequestDTO;
import cn.bugDance.infrastructure.gateway.dto.WeiXinTicketResponseDTO;
import cn.bugDance.infrastructure.persistent.redis.IRedisService;
import cn.bugDance.types.common.Constants;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import retrofit2.Call;

import javax.annotation.Resource;

@Component
@Slf4j
public class WeiXinLoginAdapter implements IWeiXinLoginAdapter {

    @Resource
    private IWeiXinApiService weiXinApiService;

    @Resource
    private IRedisService redisService;

    @Value("${wx.token.appid}")
    private String appId;

    @Value("${wx.token.grant_type}")
    private String grantType;

    @Value("${wx.token.secret}")
    private String secret;

    @Override
    public String createQrCodeTicket() {
        try{
            String cacheKey = Constants.Redis.ACCESS_TOKEN + appId;
            String access_token = redisService.getValue(cacheKey);
            if(null == access_token){
                Call<WeiXinAccessTokenResponseDTO> call = weiXinApiService.getToken(grantType, appId, secret);
                WeiXinAccessTokenResponseDTO response = call.execute().body();
                if(null == response){
                    throw new AppException(ResponseCode.UN_ERROR.getCode(), ResponseCode.UN_ERROR.getInfo());
                }
                access_token = response.getAccess_token();
                redisService.setValue(cacheKey, access_token, response.getExpires_in());
            }

            WeiXinTicketRequestDTO request = WeiXinTicketRequestDTO.builder()
                    .expire_seconds(2592000) //30天
                    .action_name(WeiXinTicketRequestDTO.ActionNameTypeVO.QR_SCENE.getCode())
                    .action_info(WeiXinTicketRequestDTO.ActionInfo.builder()
                            .scene(WeiXinTicketRequestDTO.ActionInfo.Scene.builder()
                                    .scene_id(100601)
                                    // .scene_str("test") 配合 ActionNameTypeVO.QR_STR_SCENE
                                    .build())
                            .build())
                    .build();
            Call<WeiXinTicketResponseDTO> call = weiXinApiService.getQrCode(access_token, request);
            WeiXinTicketResponseDTO response = call.execute().body();
            if(null == response){
                throw new AppException(ResponseCode.UN_ERROR.getCode(), ResponseCode.UN_ERROR.getInfo());
            }
            return response.getTicket();
        }catch (Exception e){
            throw new AppException(ResponseCode.UN_ERROR.getCode(), ResponseCode.UN_ERROR.getInfo());
        }
    }
}
